package com.quxian.chambercommerce.admin_action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionContext;
import com.quxian.chambercommerce.base.BaseAction;
import com.quxian.chambercommerce.bean.AuthorityEntity;
import com.quxian.chambercommerce.bean.StaffInfoEntity;
import com.quxian.chambercommerce.service.LoginService;
import com.quxian.chambercommerce.service.StaffAuthService;

public class RegisterStaffAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LoginService loginService;
	private StaffAuthService staffAuthService;

	private List<AuthorityEntity> listAuth;
	private List<StaffInfoEntity> listStaff;
	private StaffInfoEntity staffInfo;
	private Integer id;
	private String title;
	private String userName;
	private String password;
	private String newPassword;
	private Integer[] auth;
	private String errorMessage;
	
	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public StaffAuthService getStaffAuthService() {
		return staffAuthService;
	}

	public void setStaffAuthService(StaffAuthService staffAuthService) {
		this.staffAuthService = staffAuthService;
	}

	public List<AuthorityEntity> getListAuth() {
		return listAuth;
	}

	public void setListAuth(List<AuthorityEntity> listAuth) {
		this.listAuth = listAuth;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer[] getAuth() {
		return auth;
	}

	public void setAuth(Integer[] auth) {
		this.auth = auth;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public List<StaffInfoEntity> getListStaff() {
		return listStaff;
	}

	public void setListStaff(List<StaffInfoEntity> listStaff) {
		this.listStaff = listStaff;
	}

	public StaffInfoEntity getStaffInfo() {
		return staffInfo;
	}

	public void setStaffInfo(StaffInfoEntity staffInfo) {
		this.staffInfo = staffInfo;
	}

	public String getAllAuth(){
		try {
			listAuth = staffAuthService.getAllAuth();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String registerStaff(){
		StaffInfoEntity staff = new StaffInfoEntity();
		staff.setGrade(0);
		staff.setTitle(title);
		staff.setUser_name(userName);
		staff.setPassword(password);
		staff.setAuths(auth);
		try {
			loginService.registerStaff(staff);
		} catch (Exception e) {
			e.printStackTrace();
			errorMessage = e.getMessage();
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String updateStaffAuth(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("staffId", id);
		map.put("auth", auth);
		try {
			loginService.updateStaffAuth(map);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	public String getALlStaff(){
		try {
			listStaff = loginService.getAllStaff();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String toUpdateStaffPage(){
		
		try {
			staffInfo = loginService.getStaffById(id);
			listAuth = staffAuthService.getAllAuth();
			for(int i=0;i<listAuth.size();i++){
				
				for(int j=0;j<staffInfo.getAuths().length;j++){
					if(listAuth.get(i).getId()==staffInfo.getAuths()[j]){
						listAuth.get(i).setIsChecked(true);
						break;
					}else{
						listAuth.get(i).setIsChecked(false);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	public void checkUserNameOnly(){
		HttpServletResponse res = (HttpServletResponse) ActionContext
				.getContext().get(
						org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
		res.setContentType("text/json;charset=utf-8");
		res.setCharacterEncoding("UTF-8");
		try {
			System.out.println("====userName==="+userName);
			Integer count = loginService.checkStaffOnly(userName);
			res.getWriter().print(count);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				res.getWriter().print("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
